﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHospital.BLL;

namespace MyHospital.web.MyHospital
{
    public partial class Login : System.Web.UI.Page
    {
        User user = new User();
        protected void Page_Load(object sender, EventArgs e)
        {
            Session.Clear();
        }
        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (inp1.Value.Substring(0, 5) == "admin")
                {
                    if (user.LonginAdmin(inp1.Value, txtPwd.Text) != 0)
                    {
                        Session["AdminID"] = user.LonginAdmin(inp1.Value, txtPwd.Text);
                        Session["AdminName"] = inp1.Value;
                        Response.Redirect("~/index.aspx");
                    }
                    else lblMsg.Text = "账号或密码有误";
                }
                else
                {
                    if (user.isEmailExit(inp1.Value) == true && user.isPhoneExit(inp1.Value) == true)
                    {
                        lblMsg.Text = "账号有误！";
                    }
                    else if (user.isEmailExit(inp1.Value) == false)//邮箱登录
                    {
                        if (user.LoginEmail(inp1.Value, txtPwd.Text) != 0)
                        {

                            //Session["PatientName"] = txtName.Text;
                            int patientId = user.LoginEmail(inp1.Value, txtPwd.Text);
                            Session["PatientID"] = patientId;
                            Session["PatientName"] = user.findNameOfId(patientId);
                            Response.Redirect("~/index.aspx");
                        }
                        else lblMsg.Text = "密码错误";
                    }
                    else if (user.isPhoneExit(inp1.Value) == false)//手机号登录
                    {
                        if (user.LoginPhone(inp1.Value, txtPwd.Text) != 0)
                        {
                            
                            int patientId= user.LoginPhone(inp1.Value, txtPwd.Text);
                            Session["PatientID"] = patientId;
                            Session["PatientName"] = user.findNameOfId(patientId);
                            Response.Redirect("~/index.aspx");
                        }
                        else lblMsg.Text = "密码错误";
                    }
                }
            }
            
        }
    }
}